// \u4e00-\u9fa5] 中文验证规则
import wxValidate from '../../../miniprogram_npm/wechat-validate/index'
Page({
  behaviors: [wxValidate],
  rules: {
    name: [
      { required: true, message: '请输入业主姓名' },
      { pattern: /^[\u4e00-\u9fa5]{2,5}$/, message: '请输入中文' }
    ],
    mobile: [
      { required: true, message: '请输入业主手机号' },
      { pattern: /^1[3-8]\d{9}$/, message: '请输入正确的手机号' }
    ],
    idcardFrontUrl: [
      { required: true, message: '请上传身份证人像面' }
    ],
    idcardBackUrl: [
      { required: true, message: '请上传身份证国徽面' }
    ]
  },

  data: {
    name: '',
    gender: '1',
    mobile: '',
    idcardFrontUrl: '',
    idcardBackUrl: '',
  },
  onLoad({ point, building, room }) {
    this.setData({ point, building, room })

  },
  async goSubmit() {
    if (!this.validate()) return
    const { __webviewId__, ...data } = this.data
    const { code } = await wx.http.post('/room', data)
    if (code !== 10000) return wx.utils.toast()
    //  返回列表页
    wx.navigateBack({
      delta: 4
    })

  },
  async uploadPicture(ev) {
    console.log(ev.mark.type);
    try {
      const media = await wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sizeType: ['compressed']
      })
      console.log(media);
      wx.uploadFile({
        filePath: media.tempFiles[0].tempFilePath,
        name: 'file',
        url: wx.http.baseURL + '/upload',
        header: {
          Authorization: 'Bearer ' + getApp().token
        },
        success: (res) => {
          const data = JSON.parse(res.data)
          if (data.code !== 10000) return wx.utils.toast('上传失败!')
          // 保存图片,本地预览
          this.setData({
            [ev.mark.type]: data.data.url
          })


        }
      })
    } catch (error) {
      console.log(error);

    }



  },
  removePicture(ev) {
    // 移除图片的类型（身份证正面或反面）
    const type = ev.mark?.type
    this.setData({ [type]: '' })
  },
})
